package com.ftg.learn.controller;

import com.ftg.learn.common.dto.MemberDto;
import com.ftg.learn.common.util.JwtUtil;
import com.ftg.learn.common.util.KeyPairUtil;
import com.ftg.learn.common.util.R;
import com.ftg.learn.common.util.SecretBeanUtil;
import com.ftg.learn.common.vo.MemberVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ftg.learn.service.MemberService;

import java.security.PublicKey;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
/**
 * @Author 李孝辉
 * @Date 2025/6/16 10:31
 * @email 1626285873@qq.com
 * @Phone 18724505266
 * @内容
 */
@RestController
@RequestMapping("/member")
public class MemberController{

    @Resource
    private MemberService memberService;


    @Autowired
    SecretBeanUtil secretBeanUtil;

    @GetMapping("/showAll")
    public R<List<MemberVo>> showAll(@RequestHeader String token) {
        String publicKeyPEM = secretBeanUtil.getPublicKey();

        PublicKey publicKey = KeyPairUtil.getPublicKey(publicKeyPEM);
        Map<String,Object> mmap = JwtUtil.getClaims(token, publicKey);
        System.out.println(mmap+"===>token解析");
        return R.success(memberService.getAllMember());
    }

    @PostMapping("/addMember")
    public R<Integer> addMember(MemberDto memberDto) {
        return R.success(memberService.addMember(memberDto));
    }

    @PutMapping("/updateMember/{id}")
    public R<Integer> updateMember(MemberDto memberDto) {
        return R.success(memberService.updateMember(memberDto));
    }

    @PutMapping("/deleteMember/{id}")
    public R<Integer> deleteMember(@PathVariable("id") Long id) {
        return R.success(memberService.deleteMember(id));
    }

    @PostMapping("/TimeIntegral")
    //用于接收前端传来的时间以"秒"为单位,最后返回积分给前端展示
    public R<Integer> TimeIntegral(int time, Long id) {
        return memberService.TimeIntegral(time,id);
    }

    @PostMapping("/answerPoints")
    public R<List<Integer>> answerPoints(int topic, int userId) {
        return memberService.answerPoints(topic,userId);
    }

    @GetMapping("/SetThreshold")
    public void SetThreshold( @RequestParam String query) {
        memberService.SetThreshold(query);
    }


}
